package com.qys.livesMall.shop.service.impl;

import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.shop.enums.ShopMessageEnum;
import com.qys.livesMall.shop.modal.dto.shop.income.EditHierarchyIncomeConfig;
import com.qys.livesMall.shop.modal.entity.ShopIncomeConfig;
import com.qys.livesMall.shop.mapper.ShopIncomeConfigMapper;
import com.qys.livesMall.shop.modal.vo.H2ShopHierarchyIncomeConfigVO;
import com.qys.livesMall.shop.modal.vo.ShopIncomeConfigVO;
import com.qys.livesMall.shop.service.H2ShopHierarchyIncomeConfigService;
import com.qys.livesMall.shop.service.H2ShopLevelService;
import com.qys.livesMall.shop.service.ShopIncomeConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 店铺收益分佣配置 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-06-27
 */
@Service
public class ShopIncomeConfigServiceImpl extends ServiceImpl<ShopIncomeConfigMapper, ShopIncomeConfig> implements ShopIncomeConfigService {

    @Autowired
    private H2ShopHierarchyIncomeConfigService hierarchyIncomeConfigService;

    @Autowired
    private H2ShopLevelService shopLevelService;

    @Autowired
    private MessageServer messageServer;

    @Override
    public ShopIncomeConfig get(){
        List<ShopIncomeConfig> list= list();
        if (CollectionUtils.isEmpty(list)){
            ShopIncomeConfig config=new ShopIncomeConfig();
            config.setAnchor(new BigDecimal("0"));
            config.setPlatform(new BigDecimal("0"));
            config.setEffectiveShop(new BigDecimal("0"));
            return config;
        }
        return list.get(0);
    }
    @Override
    public R<ShopIncomeConfigVO> getConfig() {
        ShopIncomeConfig config=get();
        ShopIncomeConfigVO configVO = BeanProUtils.copyProperties(config, ShopIncomeConfigVO::new);
        configVO.setHierarchies(hierarchyIncomeConfigService.listAll());
        return R.ok(configVO);
    }

    @Override
    public R platform(BigDecimal platform) {
        ShopIncomeConfig config=get();
        BigDecimal total=config.getAnchor().add(config.getSuperiorStore()).add(platform);
        if(total.compareTo(new BigDecimal("1"))==1){
            return messageServer.failMsg(ShopMessageEnum.SHOP_INCOME_TOTAL_GE_ONE);
        }
        config.setPlatform(platform);
        saveOrUpdate(config);
        return R.ok();
    }

    @Override
    public R anchor(BigDecimal anchor) {
        ShopIncomeConfig config=get();
        BigDecimal ratio=shopLevelService.getMaxPlatformRatio();
        if (ratio==null){
            ratio=new BigDecimal("0");
        }
        BigDecimal total=ratio.add(config.getSuperiorStore()).add(anchor);
        if(total.compareTo(new BigDecimal("1"))==1){
            return messageServer.failMsg(ShopMessageEnum.SHOP_INCOME_TOTAL_GE_ONE);
        }
        config.setAnchor(anchor);
        saveOrUpdate(config);
        return R.ok();
    }

    @Override
    @Transactional
    public R addHierarchy(EditHierarchyIncomeConfig editConfig) {
        hierarchyIncomeConfigService.save(editConfig);
        BigDecimal totalH=new BigDecimal("0");
        List<H2ShopHierarchyIncomeConfigVO> vos = hierarchyIncomeConfigService.listAll();
        vos.stream().forEach(o->{
            totalH.add(o.getDividendRatio());
        });
        ShopIncomeConfig config=get();
        BigDecimal ratio=shopLevelService.getMaxPlatformRatio();
        if (ratio==null){
            ratio=new BigDecimal("0");
        }
        if(totalH.add(ratio).add(config.getAnchor()).compareTo(new BigDecimal("1"))==1){
            throw new QysException(ShopMessageEnum.SHOP_INCOME_TOTAL_GE_ONE);
        }
        config.setSuperiorStore(totalH);
        saveOrUpdate(config);
        return R.ok();
    }

    @Override
    public R editHierarchy(EditHierarchyIncomeConfig config) {
        return addHierarchy(config);
    }

    @Override
    public R delHierarchy(Integer id) {
        hierarchyIncomeConfigService.removeById(id);
        BigDecimal totalH=new BigDecimal("0");
        List<H2ShopHierarchyIncomeConfigVO> vos = hierarchyIncomeConfigService.listAll();
        if (!CollectionUtils.isEmpty(vos)){
            vos.stream().forEach(o->{
                totalH.add(o.getDividendRatio());
            });
        }
        ShopIncomeConfig config=get();
        BigDecimal ratio=shopLevelService.getMaxPlatformRatio();
        if (ratio==null){
            ratio=new BigDecimal("0");
        }
        if(totalH.add(ratio).add(config.getAnchor()).compareTo(new BigDecimal("1"))==1){
            throw new QysException(ShopMessageEnum.SHOP_INCOME_TOTAL_GE_ONE);
        }
        config.setSuperiorStore(totalH);
        saveOrUpdate(config);
        return R.ok();
    }

    @Override
    public R advancePayment(BigDecimal advancePayment) {
        ShopIncomeConfig config=get();
        if(advancePayment.compareTo(new BigDecimal("1"))==1 || advancePayment.compareTo(new BigDecimal("0"))==-1){
            return messageServer.failMsg(ShopMessageEnum.SHOP_ADVANCE_PAYMENT_DISCOUNT);
        }
        config.setAdvancePayment(advancePayment);
        saveOrUpdate(config);
        return R.ok();
    }

    @Override
    public BigDecimal getAdvancePaymentConfig() {
        List<ShopIncomeConfig> list= list();
        if (CollectionUtils.isEmpty(list)){
            return BigDecimal.ZERO;
        }
        return list.get(0).getAdvancePayment();
    }

    @Override
    public boolean checkPlatformRatio(BigDecimal decimal) {
        ShopIncomeConfig config=get();
        BigDecimal total=config.getAnchor().add(config.getSuperiorStore()).add(decimal);
        if(total.compareTo(new BigDecimal("1"))==1){
            throw  new QysException(ShopMessageEnum.SHOP_INCOME_TOTAL_GE_ONE);
        }
        return true;
    }

    @Override
    public R effectiveShop(BigDecimal effectiveShop) {
        ShopIncomeConfig config=get();
        config.setEffectiveShop(effectiveShop);
        saveOrUpdate(config);
        return R.ok();
    }
}
